package fun.boomcat.luckyhe.mirai.plugin.botcontrol.util;

import net.mamoe.mirai.Bot;
import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.contact.ContactList;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.GroupSettings;
import net.mamoe.mirai.contact.NormalMember;

import java.util.Arrays;

/**
 * @author Lucky_He
 * @date 2021年8月20日 0:11
 * @description 群操作相关
 */
public class GroupOperation {

    public static void groupOperation(CommandSender commandSender, Object[] args, Group group, String primaryName) {
//        对群的操作
        int len = args.length;

        String operation = args[0].toString();

        switch (operation.toLowerCase()) {
            case "help":
            case "帮助":

                commandSender.sendMessage(ControlCommandHelp.groupOperationHelp(primaryName, group));
                break;

            case "member":
            case "成员":

                groupMemberSelection(commandSender, Arrays.copyOfRange(args, 1, len), group, primaryName);
                break;

            case "settings":
            case "设置":

                groupSettings(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        group,
                        primaryName
                );
                break;

            case "send":
            case "发送":

                groupSend(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        group
                );

                break;

            case "quit":
            case "退出":
                groupQuit(
                        commandSender,
                        group
                );
                break;

            default:
                commandSender.sendMessage(ControlCommandHelp.groupOperationHelp(primaryName, group));
                return;
        }
    }

    public static void groupMemberSelection(CommandSender commandSender, Object[] args, Group group, String primaryName) {
//        对群成员的选择

        Bot bot = BotUtil.getBotInstance();

        int len = args.length;
        ContactList<NormalMember> members = group.getMembers();
        if (len == 0) {
            BotUtil.pageSender(commandSender, BotUtil.getGroupMembersStr(members, group, primaryName));
            return;
        }

        long qq;
        try {
            qq = Long.parseLong(args[0].toString());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        NormalMember member = null;
        for (NormalMember m : members) {
            if (m.getId() == qq) {
                member = m;
                break;
            }
        }

//        经实践，members中不存在bot本身
//        因此需要特别判断bot
        assert bot != null;
        if (qq == bot.getId()) {
            member = group.getBotAsMember();
        }

        if (member == null) {
            commandSender.sendMessage("群" + group.getId() + "无QQ号为" + qq + "的成员");
            return;
        }

        if (len == 1) {
//            BotUtil.pageSender(commandSender, BotUtil.getGroupMemberMulLineStr(member, primaryName));
            commandSender.sendMessage(BotUtil.getGroupMemberMulLineStr(member, primaryName));
        } else {
            GroupMemberOperation.groupMemberOperation(commandSender, Arrays.copyOfRange(args, 1, len), member, primaryName);
        }
    }

    public static void groupSettings(CommandSender commandSender, Object[] args, Group group, String primaryName) {
//        群设置
        int len = args.length;
        GroupSettings settings = group.getSettings();

        if (len == 0) {
            commandSender.sendMessage(BotUtil.getGroupSettingsMulLineStr(settings, group, primaryName));

        } else {
            GroupSettingOperation.groupSettingOperation(commandSender, args, settings, group);
        }
    }

    public static void groupSend(CommandSender commandSender, Object[] args, Group group) {
//        向群发送消息
        StringBuilder msg = new StringBuilder();
        for (Object arg : args) {
            msg.append(arg.toString()).append(" ");
        }

        try {
            group.sendMessage(msg.toString().trim());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已向群" + group.getId() + "发送消息：\n" + msg.toString().trim());
    }

    public static void groupQuit(CommandSender commandSender, Group group) {
//        退出群
        try {
            group.quit();
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("退出群" + group.getId() + "成功");
    }

}
